# Copyright (c) 2011 Antoine d'Otreppe de Bouvette
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

import unittest2
from copy import copy, deepcopy

from aspyct.aop import Aspect, CompositeAspect

def doNothing(*args, **kwargs): pass

class TestAspect(Aspect):
    def __init__(self):
        self.callMe_atCall = doNothing
        self.callMe_atReturn = doNothing
        self.callMe_atRaise = doNothing

    def atCall(self, flow):
        self.atCallFlow = deepcopy(flow)
        self.callMe_atCall(flow)

    def atReturn(self, flow):
        self.atReturnFlow = deepcopy(flow)
        self.callMe_atReturn(flow)

    def atRaise(self, flow):
        self.atRaiseFlow = deepcopy(flow)
        self.callMe_atRaise(flow)

class AspectTest(unittest2.TestCase):
    def setUp(self):
        self.aspect = TestAspect()

    def tearDown(self):
        if hasattr(self, 'witness'):
            del self.witness

    def test_callsAtCallBeforeExecution(self):
        def assertion(flow):
            self.assertFalse(hasattr(self, 'witness'),
                "The witness must not exist")
        self.aspect.callMe_atCall = assertion

        @self.aspect
        def myTest():
            self.witness = "Hello"

        myTest()

    def test_callsAtCallBeforExecution2(self):
        def act(flow):
            self.witness = True

        self.aspect.callMe_atCall = act

        @self.aspect
        def myTest(): pass

        myTest()
        self.assertTrue(hasattr(self, 'witness'), "The witness must exist")
    
    def test_callsAtReturnAfterNormalExecution(self):
        def act(flow):
            self.witness = True
        
        self.aspect.callMe_atReturn = act

        @self.aspect
        def myTest(): pass

        myTest()
        self.assertTrue(hasattr(self, 'witness'), "The witness must exist")

    def test_callsAtReturnAfterNormalExecution2(self):
        def assertion(flow):
            self.assertTrue(hasattr(self, 'witness'),
                "The witness must exist")

        self.aspect.callMe_atReturn = assertion

        @self.aspect
        def myTest():
            self.witness = True


    def test_doesNotCallAtReturnUponRaising(self):
        pass

class TestCompositeAspect(CompositeAspect):
    def __init__(self):
        self.aspect = TestAspect()
        CompositeAspect.__init__(self, self.aspect)

    def __setattr__(self, attr, value):
        setattr(self.aspect, attr, value)

class CompositeAspectTest(AspectTest):
    def setUp(self):
        AspectTest.setUp(self)
        self.aspect = CompositeAspect(TestAspect())

if __name__ == "__main__":
    unittest2.main()

